package billings.classtasks;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import billings.classtasks.classes.Class;
import billings.classtasks.classes.EditClass;
import billings.classtasks.tasks.CreateTask;
import billings.classtasks.tasks.EditTask;
import billings.classtasks.tasks.Task;

public class ExpandableListAdapter extends BaseExpandableListAdapter {


    private Map<Integer, Class> classMap;
    private Map<Integer, List<Task>> taskMap;
    private Context context;
    private LayoutInflater groupInflater;
    private LayoutInflater childInflater;
    
    public ExpandableListAdapter(List<Class> classes, List<Task> tasks, Context context){
    	classMap = new HashMap<Integer, Class>();
    	taskMap = new HashMap<Integer, List<Task>>();
    	this.context = context;
    	groupInflater = LayoutInflater.from(context);
    	childInflater = LayoutInflater.from(context);
    	
    	for(Class c : classes){
    		classMap.put(c.getId(), c);
    		taskMap.put(c.getId(), new ArrayList<Task>());
    	}

    	for(Task t : tasks){
    		List<Task> tmpTasks = null;
    		if(taskMap.get(t.getClassId()) != null){
    			tmpTasks = taskMap.get(t.getClassId());
    		}else{
    			tmpTasks = new ArrayList<Task>();
    		}
    		
    		tmpTasks.add(t);
    		
    		taskMap.put(t.getClassId(), tmpTasks);
    	}
    }
    
    private Integer iterateToGroupPosition(int groupPosition){
    	Iterator<Integer> topKeyIter = classMap.keySet().iterator();
    	Integer topKeyFinal = 0;
    	ListIterator<Task> iter = null;
    	for(int i = 0; i <= groupPosition; i++){
    		topKeyFinal = topKeyIter.next();
    	}
    	return topKeyFinal;
    }
    
    public Object getChild(int groupPosition, int childPosition) {
    	return taskMap.get(iterateToGroupPosition(groupPosition)).get(childPosition);
    }

	public long getChildId(int groupPosition, int childPosition) {
		return taskMap.get(iterateToGroupPosition(groupPosition)).get(childPosition).getId();
	}

	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {		
		View v = childInflater.inflate(R.layout.task_row, null);		
		
		TextView tv = (TextView) v.findViewById(R.id.taskText);			
		Task chosenTask = (Task)getChild(groupPosition, childPosition);					
		tv.setText(chosenTask.toStringTruncate());
		
		final int chosenTaskId = chosenTask.getId();
		final String chosenTaskTitle = chosenTask.getTitle();
		
		String[] dateSplit = chosenTask.getDueDate().split("/");
		final int dueDateMonth = Integer.valueOf(dateSplit[0]);
		final int dueDateDate = Integer.valueOf(dateSplit[1]);
		final int dueDateYear = Integer.valueOf(dateSplit[2]);
		
		final String chosenTaskDescription = chosenTask.getDescription();
		final int chosenTaskClass = chosenTask.getClassId();
		
		//edit button
		ImageView ib1 = (ImageView)v.findViewById(R.id.groupEditTask);
		ib1.setId(R.id.groupEditTask+chosenTask.getId());
		ib1.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent i = new Intent(context, EditTask.class);
				i.putExtra("taskId", chosenTaskId);
				i.putExtra("taskTitle", chosenTaskTitle);
				i.putExtra("taskDescription", chosenTaskDescription);
				i.putExtra("dueDateYear", dueDateYear);
				i.putExtra("dueDateMonth", dueDateMonth);
				i.putExtra("dueDateDate", dueDateDate);
				i.putExtra("classId", chosenTaskClass);
				
				context.startActivity(i);				
			}
		});
		
		//delete button
		ImageView ib2 = (ImageView)v.findViewById(R.id.groupDeleteTask);
		ib2.setId(R.id.groupDeleteTask+chosenTask.getId());
		ib2.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				
				Intent i = new Intent(context, ClassTasksActivity.class);
				i.putExtra("action", "deleteTask");
				i.putExtra("taskId", chosenTaskId);
				context.startActivity(i);
			}
		});
		
		ib2.setEnabled(false);
		
		//on long click, enable deleting
		v.setOnLongClickListener(new View.OnLongClickListener() {
			public boolean onLongClick(View v) {
				
				ImageView iv = (ImageView)v.findViewById(R.id.groupDeleteTask+chosenTaskId);
				if(iv.isEnabled()){
					iv.setEnabled(false);
					iv.setVisibility(View.INVISIBLE);
					v.setBackgroundColor(Color.BLACK);
				}else{
					iv.setEnabled(true);
					iv.setVisibility(View.VISIBLE);
					v.setBackgroundColor(Color.RED);
				}
				return true;
			}
		});
		
		return v;
	}
	
	public int getChildrenCount(int groupPosition) {
		if(taskMap.isEmpty()){
			return 0;
		}
		return taskMap.get(iterateToGroupPosition(groupPosition)).size();
	}

	public Object getGroup(int groupPosition) {
		return classMap.get(iterateToGroupPosition(groupPosition));
	}

	public int getGroupCount() {
		return classMap.entrySet().size();
	}

	public long getGroupId(int groupPosition) {
		return classMap.get(iterateToGroupPosition(groupPosition)).getId();
	}

	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		View v = groupInflater.inflate(R.layout.class_row, null);		
		
		TextView tv = (TextView) v.findViewById(R.id.classText);			
		Class chosenClass = (Class)getGroup(groupPosition);					
		tv.setText(chosenClass.toString());
		tv.setId(R.id.classText+chosenClass.getId());
		
		final int chosenClassId = chosenClass.getId();		
		
		ImageView ib = (ImageView)v.findViewById(R.id.groupAddTask);
		ib.setId(R.id.groupAddTask+chosenClass.getId());
		ib.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent i = new Intent(context, CreateTask.class);
				i.putExtra("classId", chosenClassId);
				context.startActivity(i);
			}
		});				
		
		return v;
	}

	public boolean hasStableIds() {
		return true;
	}

	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

}
